﻿#region Using statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace Rectangular.Model.Environment
{
    public class TileMap
    {
        #region Members
        /* Represents all the tiles in the map */
        private Tile[,] _tiles;
        #endregion

        #region Constructors
        /// <summary>
        /// Constructs a new tile map
        /// </summary>
        public TileMap(int rows, int cols)
        {
            _tiles = new Tile[rows, cols];
        }
        #endregion

        #region Properties
        /// <summary>
        /// Tiles property
        /// </summary>
        public Tile[,] Tiles
        {
            get { return _tiles; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Draws the tile map
        /// </summary>
        /// <param name="spriteBatch">The sprite batch which is used to draw</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            for (int row = 0; row < _tiles.GetLength(0); row++)
            {
                for (int col = 0; col < _tiles.GetLength(1); col++)
                {
                    if (_tiles[row, col] != null)
                    {
                        Tile tile = _tiles[row, col];
                        tile.Position = new Vector2(col * tile.Width + Camera.Position.X, row * tile.Height + Camera.Position.Y);

                        if (tile.Position.X >= Camera.Position.X - tile.Width &&
                            tile.Position.X <= Camera.Position.X + Camera.ScreenWidth)
                        {
                            tile.Draw(spriteBatch);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Converts a text based map to an array of tiles
        /// </summary>
        /// <param name="tiles">An array that is a text based representation of the map</param>
        public void LoadMap(int[,] tiles)
        {
            int rows = tiles.GetLength(0);
            int cols = tiles.GetLength(1);

            // Create new list of tiles
            _tiles = new Tile[rows, cols];

            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    if (tiles[row, col] != -1)
                    {
                        // Create new tile
                        Tile tile = new Tile(tiles[row, col]);

                        // Add tile to list
                        _tiles[row, col] = tile;
                    }
                }
            }
        }

        /// <summary>
        /// Returns a string representation of the object
        /// </summary>
        /// <returns>A string representation of the object</returns>
        public string PrintMap()
        {
            string s = "";
            for (int row = 0; row < _tiles.GetLength(0); row++)
            {
                for (int col = 0; col < _tiles.GetLength(1); col++)
                {
                    if (_tiles[row, col] != null)
                    {
                        s += _tiles[row, col].ToString();
                    }
                }
                s += "\n";
            }
            return s;
        }
        #endregion
    }
}